package servlet;

import dao.AdminDAO;
import dao.UserDAO;
import dao.UserDAO1;
import entity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "userAdd", value = "/admin/add")
public class RegisterServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置post请求参数乱码问题
        req.setCharacterEncoding("UTF-8");

        // 接收请求参数
        String username = req.getParameter("username");

        String password = req.getParameter("password");


        // 封装成一个User对象
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);

        // 创建dao
        UserDAO1 dao = new UserDAO1();
        User byName = dao.findByName(username);
        if(byName != null) {
            System.out.println("用户" + username + "重名");
            return;
        }
        dao.insert(user);

        System.out.println(user);

        // 重定向
        resp.sendRedirect("/Mall/index.jsp");
    }
}
